package com.htdc.web.service;


import com.htdc.web.bean.Agreement;
import com.htdc.web.bean.Bill;
import com.htdc.web.dao.AgreementDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Service
public class AgreementService {

    @Autowired
    private AgreementDao agreementDao;
    @Autowired
    private BillService billService;

    //终止合同
    public boolean stopAgreement(String aid){
        if(agreementDao.getAgreementByAid(aid)!=null){
            //改合同
            Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
            agreementDao.stopAgreement(aid,sqlDate);
            //改账单
            billService.updateBillByAid(aid,sqlDate);
            return true;
        }
        System.err.println("找不到要中止的合同");
        return false;
    }

    //展示所有异常合同
    public List<Agreement> showAllAbnormalAgreements(){
        Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
        List<Bill>billList=billService.showAllArrearage(sqlDate);
        List<Agreement> agreements=new ArrayList<>();
        for (Bill bill:billList){
            agreements.add(agreementDao.getAgreementByAid(bill.getAid()));
        }
        return agreements;
    }

    //添加合同
    public boolean addAgreements(Agreement agreement){

        if(agreement!=null) {
            agreementDao.addAgreement(agreement);

            return true;
        }
        return false;
    }

    //根据RID查看历史合同
    public List<Agreement> show(Integer rid){

        return  agreementDao.showAllAgreementsByRid(rid);
    }

    //通过项目获取所有合同
    public List<Agreement> showAllAgreementByPid(Integer pid){

        return agreementDao.getAgreementsByProjectID(pid);
    }

}
